package com.jinlejia.erp.utils;

import android.content.Context;
import android.content.Intent;

import com.freedom.yefeng.yfrecyclerview.YfListAdapter;
import com.freedom.yefeng.yfrecyclerview.YfListMode;
import com.jinlejia.erp.R;
import com.jinlejia.erp.bean.SaveCookieBean;
import com.jinlejia.erp.ui.common.LoginActivity;
import com.jinlejia.erp.ui.common.NoPermissionDialog;
import com.jinlejia.erp.ui.mine.transfer.AuditDialog;
import com.weight.CustomToast;

import java.util.Map;


/**
 * 数据筛选
 */

public class DataFilter {

    public static boolean filter(Context context, Map<String, Object> map, YfListAdapter adapter) {
        boolean flag = false;
        if (Integer.parseInt(map.get("code").toString()) == 200) {
            flag = true;
            if (adapter != null)
                adapter.removeAllFooters();
        } else if (Integer.parseInt(map.get("code").toString()) == 300) {
            //有未审核的移动申请
            flag = false;
            new AuditDialog(context).show();
        } else if (Integer.parseInt(map.get("code").toString()) == 303) {
            //没有更多
            flag = false;
            if (adapter != null) {
                adapter.removeAllFooters();
                if (adapter.getData().size() > 10)
                    adapter.addFooter(R.string.is_all);
            }
        } else if (Integer.parseInt(map.get("code").toString()) == 501) {
            //没有数据
            if (adapter != null)
                adapter.changeMode(YfListMode.MODE_EMPTY);
        } else if (Integer.parseInt(map.get("code").toString()) == 700) {
            //没有权限
            if (adapter != null) {
                adapter.changeMode(YfListMode.MODE_ERROR);
            } else {
                new NoPermissionDialog(context).show();
            }
        } else if (Integer.parseInt(map.get("code").toString()) == 800) {
            //登录名失效
//            ACache.get(context).put("cookie", "");
            DbUtil.getDB(context).single().delete(SaveCookieBean.class);
            Intent intent = new Intent(context, LoginActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.getApplicationContext().startActivity(intent);
        } else {
            if (map.get("message") != null)
                CustomToast.showToast(context, map.get("message").toString(), 1500);
        }
        return flag;
    }

    public static boolean filter(Context context, Map<String, Object> map) {
        return DataFilter.filter(context, map, null);
    }

}
